package top.xin1901.j2ecd.service.impl;

import org.springframework.stereotype.Service;
import top.xin1901.j2ecd.beans.User;
import top.xin1901.j2ecd.dao.UserDao;
import top.xin1901.j2ecd.service.UserService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author HeYunjia
 */

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public User insertOne(User user) {
        Date date = new Date();
        user.setGmtCreate(date);
        user.setGmtModified(date);

        return userDao.insert(user) == 1
                ? userDao.selectOne(user.getUserId())
                : null;
    }

    @Override
    public boolean deleteByPrimaryKey(Long userId) {
        return userDao.deleteByPrimaryKey(userId) == 1;
    }

    @Override
    public User updateByPrimaryKey(User user) {
        user.setGmtModified(new Date());
        return userDao.updateByPrimaryKey(user) == 1
                ? userDao.selectOne(user.getUserId())
                : null;
    }

    @Override
    public List<User> selectAll() {
        return userDao.selectAll();
    }

    @Override
    public User selectOne(Long userId) {
        return userDao.selectOne(userId);
    }
}
